# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: OSAMC <https://github.com/osam-cologne/archlinux-proaudio>
# Contributor: Bleuzen <supgesu at gmail dot com>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Florian Hülsmann <fh@cbix.de>

pkgbase=surge-xt
pkgname=(
  'surge-xt'
  'surge-xt-common'
  'surge-xt-standalone'
  'surge-xt-vst3'
  'surge-xt-clap'
  'surge-xt-lv2'
)
pkgver=1.2.3
pkgrel=2
pkgdesc='Subtractive hybrid synthesizer'
arch=('loong64' 'x86_64')
url='https://surge-synthesizer.github.io'
license=('GPL3')
groups=('pro-audio')
_common_depends=(
  'glibc'
  'gcc-libs'
  'cairo'
  'fontconfig'
  'freetype2'
  'libx11'
  'libxcb'
  'libxkbcommon-x11'
  'xcb-util'
  'xcb-util-cursor'
  'xcb-util-image'
  'xcb-util-keysyms'
  'xcb-util-renderutil'
  'xdg-utils'
  'fmt'
  'libsamplerate'
)
makedepends=(
  "${_common_depends[@]}"
  'git'
  'cmake'
  'libsndfile'
  'python'
  'libxrandr'
  'libxinerama'
  'libxcursor'
  'alsa-lib'
  'jack'
  'webkit2gtk'
  'ripgrep'
  'lv2'
  'juce'
)
options=('!lto') # LTO disabled due to crashes at application startup
_commit='bb8dd2b398cfa8d6d1a20daf7dee2cb4e8097dbf'
source=(
  "$pkgbase::git+https://github.com/surge-synthesizer/surge#commit=$_commit"
  'improve-reproducibility.patch'
  'devendor-libs.patch'
  'fix-lv2-ftbfs.patch'
  'github.com-surge-synthesizer-tuning-library::git+https://github.com/surge-synthesizer/tuning-library'
  'github.com-surge-synthesizer-eurorack::git+https://github.com/surge-synthesizer/eurorack'
  'github.com-surge-synthesizer-JUCE::git+https://github.com/surge-synthesizer/JUCE'
  'github.com-surge-synthesizer-sst-plugininfra::git+https://github.com/surge-synthesizer/sst-plugininfra'
  'github.com-surge-synthesizer-sst-cpputils::git+https://github.com/surge-synthesizer/sst-cpputils'
  'github.com-surge-synthesizer-sst-filters::git+https://github.com/surge-synthesizer/sst-filters'
  'github.com-surge-synthesizer-sst-waveshapers::git+https://github.com/surge-synthesizer/sst-waveshapers'
  'github.com-surge-synthesizer-pffft::git+https://github.com/surge-synthesizer/pffft'
  'github.com-free-audio-clap::git+https://github.com/free-audio/clap'
  'github.com-free-audio-clap-helpers::git+https://github.com/free-audio/clap-helpers'
  'github.com-free-audio-clap-juce-extensions::git+https://github.com/free-audio/clap-juce-extensions'
  'github.com-simd-everywhere-simde::git+https://github.com/simd-everywhere/simde'
  'github.com-ODDSound-MTS-ESP::git+https://github.com/ODDSound/MTS-ESP'
  'github.com-LuaJIT-LuaJIT::git+https://github.com/LuaJIT/LuaJIT'
  'github.com-taocpp-PEGTL::git+https://github.com/taocpp/PEGTL'
  'github.com-gulrak-filesystem::git+https://github.com/gulrak/filesystem'
  'github.com-lv2-porting-project-JUCE::git+https://github.com/lv2-porting-project/JUCE#branch=lv2'
)
b2sums=('SKIP'
        '6e71b56ffde699319def99e03637d9c3fb6197a6e0637b9f44b52a9207a8e06c8d482c05cdd711ec2c5901f403381dda70e786bca6b56cce14dc449325f24761'
        '35ae92a5363334901810d801238ab9e655c2db5695d085bbc431291ef107c5726d2aa919de77fba154ab63b3875df772cc5fd1531c0adcb9215cac99500ba832'
        'c43e5a1b9605243be07ab3903885836a2199787f0c7a8dc6ae2be9d8d4a91bcf07fde520c3440f1be425194476065cb287b99446b144c5cd76d012fb0f10f35e'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP'
        'SKIP')

pkgver() {
  cd "$pkgbase"

  git describe --tags | sed 's/^release_xt_//'
}

prepare() {
  cd "$pkgbase"

  # explicitly state which submodules to use,
  # omitting devendored & unused submodules
  git submodule init \
    libs/tuning-library \
    libs/simde \
    libs/eurorack/eurorack \
    libs/oddsound-mts/MTS-ESP \
    libs/JUCE \
    libs/LuaJitLib/LuaJIT \
    libs/PEGTL \
    libs/sst/sst-plugininfra \
    libs/sst/sst-cpputils \
    libs/clap-juce-extensions \
    libs/sst/sst-filters \
    libs/sst/sst-waveshapers \
    libs/pffft

  git config submodule.libs/tuning-library.url "$srcdir/github.com-surge-synthesizer-tuning-library"
  git config submodule.libs/simde.url "$srcdir/github.com-simd-everywhere-simde"
  git config submodule.libs/eurorack/eurorack.url "$srcdir/github.com-surge-synthesizer-eurorack"
  git config submodule.libs/oddsound-mts/MTS-ESP.url "$srcdir/github.com-ODDSound-MTS-ESP"
  git config submodule.libs/JUCE.url "$srcdir/github.com-surge-synthesizer-JUCE"
  git config submodule.libs/LuaJitLib/LuaJIT.url "$srcdir/github.com-LuaJIT-LuaJIT"
  git config submodule.libs/PEGTL.url "$srcdir/github.com-taocpp-PEGTL"
  git config submodule.libs/sst/sst-plugininfra.url "$srcdir/github.com-surge-synthesizer-sst-plugininfra"
  git config submodule.libs/sst/sst-cpputils.url "$srcdir/github.com-surge-synthesizer-sst-cpputils"
  git config submodule.libs/clap-juce-extensions.url "$srcdir/github.com-free-audio-clap-juce-extensions"
  git config submodule.libs/sst/sst-filters.url "$srcdir/github.com-surge-synthesizer-sst-filters"
  git config submodule.libs/sst/sst-waveshapers.url "$srcdir/github.com-surge-synthesizer-sst-waveshapers"
  git config submodule.libs/pffft.url "$srcdir/github.com-surge-synthesizer-pffft"

  git -c protocol.file.allow=always submodule update

  # setup git submodules for clap-juce-extensions
  pushd libs/clap-juce-extensions
  git submodule init
  git config submodule.clap-libs/clap.url "$srcdir/github.com-free-audio-clap"
  git config submodule.clap-libs/clap-helpers.url "$srcdir/github.com-free-audio-clap-helpers"
  git -c protocol.file.allow=always submodule update
  popd

  # setup git submodules for sst-plugininfra
  pushd libs/sst/sst-plugininfra
  git submodule init
  git config submodule.libs/filesystem/filesystem.url "$srcdir/github.com-gulrak-filesystem"
  git -c protocol.file.allow=always submodule update
  popd

  # first shot at improving reproducibility:
  # https://github.com/surge-synthesizer/surge/issues/2389
  patch -p1 -i "$srcdir/improve-reproducibility.patch"

  # devendor libs
  patch -p1 -i "$srcdir/devendor-libs.patch"
  rg --files-with-matches 'include "fmt/core.h"' | xargs -I@ sed 's|"fmt/core.h"|<fmt/core.h>|g' -i @
  rg --files-with-matches 'include "samplerate.h"' | xargs -I@ sed 's|"samplerate.h"|<samplerate.h>|g' -i @

  # add missing LV2 header
  pushd "$srcdir/github.com-lv2-porting-project-JUCE"
  patch -p1 -i "$srcdir/fix-lv2-ftbfs.patch"
  popd

  # FTBFS due to GCC13
  git cherry-pick --no-commit d2fba1c6ff02cbdfa9db2ce06508745c247629b5
}

build() {
  # binaries and shared libs have executable stack:
  # https://github.com/surge-synthesizer/surge/issues/1701
  export LDFLAGS="$LDFLAGS,-z,noexecstack"

  cmake \
    -B build \
    -S "$pkgbase" \
    -DCMAKE_INSTALL_PREFIX='/usr' \
    -DCMAKE_INSTALL_LIBDIR='/usr/lib' \
    -DCMAKE_BUILD_TYPE=Release \
    -W no-dev \
    -DSURGE_BUILD_TESTRUNNER=OFF

  cmake --build build

  # build LV2 plugin
  cmake \
    -B build-lv2 \
    -S "$pkgbase" \
    -DCMAKE_INSTALL_PREFIX='/usr' \
    -DCMAKE_INSTALL_LIBDIR='/usr/lib' \
    -DCMAKE_BUILD_TYPE=Release \
    -W no-dev \
    -DSURGE_BUILD_TESTRUNNER=OFF \
    -DJUCE_SUPPORTS_LV2=True \
    -DSURGE_JUCE_PATH="$srcdir/github.com-lv2-porting-project-JUCE"

  cmake --build build-lv2 --target surge-xt_LV2 surge-fx_LV2 --parallel
}

package_surge-xt() {
  depends=($pkgbase-{common,standalone,vst3,clap,lv2}=$pkgver)

  # complete install, followed by…
  DESTDIR="$pkgdir" cmake --install build

  # …moving items out for split packages…
  mv "$pkgdir/usr/share/surge-xt" usr-share
  mv "$pkgdir/usr/bin" usr-bin
  mv "$pkgdir/usr/lib" usr-lib

  # …then remove leftovers
  rm -rvf "$pkgdir/usr"
}

package_surge-xt-common() {
  pkgdesc='Common files for Surge XT'

  install -vd "$pkgdir/usr/share/surge-xt"
  mv usr-share/* "$pkgdir/usr/share/surge-xt"

  cd "$pkgbase"

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgbase" AUTHORS README.md doc/*
}

package_surge-xt-standalone() {
  pkgdesc+=' - standalone'
  depends=(
    "surge-xt-common=$pkgver"
    'hicolor-icon-theme'
    "${_common_depends[@]}"
  )
  optdepends=(
    'jack: audio backend'
    'alsa-lib: audio backend'
  )

  # binaries
  install -vd "$pkgdir/usr/bin"
  mv usr-bin/* "$pkgdir/usr/bin"

  cd "$pkgbase"

  # application icons & desktop files
  install -vd "$pkgdir/usr/share/"{applications,icons}
  cd scripts/installer_linux
  cp -r assets/icons/hicolor "$pkgdir/usr/share/icons"
  cp -r assets/icons/scalable "$pkgdir/usr/share/icons/hicolor"
  install -vDm644 -t "$pkgdir/usr/share/applications" assets/applications/*.desktop
}

package_surge-xt-vst3(){
  pkgdesc+=' - VST3 plugin'
  groups+=('vst3-plugins')
  depends=(
    "surge-xt-common=$pkgver"
    "${_common_depends[@]}"
    'vst3-host'
  )

  install -vd "$pkgdir/usr/lib"
  mv usr-lib/vst3 "$pkgdir/usr/lib"
}

package_surge-xt-clap(){
  pkgdesc+=' - CLAP plugin'
  groups+=('clap-plugins')
  depends=(
    "surge-xt-common=$pkgver"
    "${_common_depends[@]}"
    'clap-host'
  )

  install -vd "$pkgdir/usr/lib"
  mv usr-lib/clap "$pkgdir/usr/lib"
}

package_surge-xt-lv2(){
  pkgdesc+=' - LV2 plugin'
  groups+=('lv2-plugins')
  depends=(
    "surge-xt-common=$pkgver"
    "${_common_depends[@]}"
    'lv2-host'
  )

  install -vd "$pkgdir/usr/lib/lv2"
  cp -vr build-lv2/surge_xt_products/*.lv2 "$pkgdir/usr/lib/lv2"
}
